package com.neusoft.lohas.common.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class FloatFormat {

	public static double m1(double f) {
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}
	
	public static double m1_1(double f) {
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	/**
	 * DecimalFormat转换最简便
	 */
	public static double m2(double f) {
		DecimalFormat df = new DecimalFormat("#.00");
		return Double.valueOf(df.format(f));
	}
	
	public static double m2_2(double f) {
		DecimalFormat df = new DecimalFormat("#.0");
		return Double.valueOf(df.format(f));
	}

	/**
	 * String.format打印最简便
	 */
	public static double m3(double f) {
		return Double.valueOf(String.format("%.2f", f));
	}
	
	public static double m3_3(double f) {
		return Double.valueOf(String.format("%.1f", f));
	}
	
	/**
	 * 显示千位符
	 */
	public static double m4(double f) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);
		return Double.valueOf(nf.format(f));
	}
	
	public static double m4_4(double f) {
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(1);
		return Double.valueOf(nf.format(f));
	}

	public static void main(String[] args) {
		double a = 111231.5585;
		System.out.println(FloatFormat.m1(a));
		System.out.println(FloatFormat.m2(a));
		System.out.println(FloatFormat.m3(a));
		System.out.println(FloatFormat.m4(a));
	}
}
